--[[

    Copyright (c) 2012-2020 baby-bus.com

    TODO:   SpriteName
    Author: DemonYao
    Date:   2020.09.21
 
    http://www.babybus.com/ 

]] 




--------------------------
-- 引入命名空间
--------------------------
-- local BaseLayer = import("app.common.layer.BaseLayer")




--[[
    类
--]]


local M = classSprite("Progress")
local dbFactory = dragonBones.CCFactory



--------------------------
-- 构造函数
--------------------------

function M:ctor(params)
    -- [超类调用]
    M.super.ctor(self, params)
    -- [本类调用]
    self.dragonBonesDataFile  = params.dragonBonesDataFile      -- 骨骼文件
    self.dragonBonesTexFile   = params.dragonBonesTexFile       -- 骨骼文件
    self.armatureName         = params.armatureName             -- 骨骼名称
    self.animName             = params.animName                 -- 初始骨骼动作名称（选填）
end

--------------------------
-- 渲染
--------------------------

function M:onRender()
    M.super.onRender(self)
    -- [本类调用]
    -- 加载节点
    self:createArmature()
end

-- 加载节点
function M:loadDisplay()
    -- 
end

-- 加载渲染以外的其他操作
function M:onEnterTransitionFinish()
    --触控 

    self:bindTouchLocate()
    -- tool:loadHelpRect({
	-- 	node = self,
	-- 	pos = cc.p(0, 0),
	-- 	size = cc.size(self:cw(), self:ch()),
	-- 	anchorPoint = cc.p(0, 0)
	-- }):opacity(200):z(-1)
end

-- 创建骨骼
function M:createArmature()
	dragonBones.CCFactory:loadDragonBonesData(self.dragonBonesDataFile, "test")
	dragonBones.CCFactory:loadTextureAtlasData(self.dragonBonesTexFile, "test")
    local armatureDisplay = dragonBones.CCFactory:buildArmatureDisplay(self.armatureName)
    CCNodeExtend.extend(armatureDisplay)
    -- 设置节点大小
    self:setContentSize(cc.size(armatureDisplay:cw(), armatureDisplay:ch()))

    armatureDisplay:anchor(cc.p(0.5, 0.5))
    local size = armatureDisplay:getContentSize()
    local anchor = armatureDisplay:getAnchorPoint()
    
    armatureDisplay:to(self):p(size.width * anchor.x, size.height * anchor.y)

    if self.animName then
        armatureDisplay:getAnimation():play(self.animName)
    end

    self:armatureBindTouch(armatureDisplay)
    self:bindDBEvent(armatureDisplay)
end

-- 触控绑定
function M:armatureBindTouch(target)
    target:bindTouch()
    --------------------------
    -- 触控
    -------------------------- 
    local parent = self    
    function target:onTouchBegan(x, y, touches)
        self._isTouched  = true
        local progress = math.min(math.max((x - self:getPositionX() + 300) / 600, 0), 1)

        -- 获取动作时长
        local actionTime = self:getAnimation():getState("idle"):getTotalTime()

        -- 从指定时间开始播放指定的动画。
        -- self:getAnimation():gotoAndPlayByTime(parent.animName, 1)
        -- 在指定时间停止指定动画播放
        -- self:getAnimation():gotoAndStopByTime(parent.animName, 0.5)


        -- 在指定帧停止指定动画的播放
        -- self:getAnimation():gotoAndStopByProgress(parent.animName, progress);
        -- 在指定的进度停止指定的动画播放。
        self:getAnimation():gotoAndPlayByProgress(parent.animName, 0.5, 0);
        return SIGN_TOUCH_BEGAN_SWALLOWS 
    end
    
    function target:onTouchMoved(x, y, touches)
        if self._isTouched and  self:getAnimation():getState("idle") 
            and not  self:getAnimation():getState("idle"):isPlaying() then

            local progress = math.min(math.max((x - self:getPositionX() + 300) / 600, 0), 1)
            self:getAnimation():gotoAndStopByProgress("idle", progress)
        end
    end
    
    function target:onTouchEnded(x, y, touches)
        self:getAnimation():play()
    end
end

-- 骨骼事件绑定
function M:bindDBEvent(armatureDisplay)
    armatureDisplay:addDBEventListener("start",  handler(self, self.animationEventHandler))

    armatureDisplay:addDBEventListener("loopComplete",  handler(self, self.animationEventHandler))

    armatureDisplay:addDBEventListener("complete",  handler(self, self.animationEventHandler))

    armatureDisplay:addDBEventListener("fadeIn",  handler(self, self.animationEventHandler))

    armatureDisplay:addDBEventListener("fadeInComplete",  handler(self, self.animationEventHandler))

    armatureDisplay:addDBEventListener("fadeOut",  handler(self, self.animationEventHandler))
 
    armatureDisplay:addDBEventListener("fadeOutComplete",  handler(self, self.animationEventHandler))

    armatureDisplay:addDBEventListener("frameEvent", handler(self, self.animationEventHandler))

    armatureDisplay:addDBEventListener("soundEvent", handler(self, self.animationEventHandler))
end

function M:animationEventHandler(eventObject)
    -- print("动作名称==="..eventObject.animationState:getName(), "事件类型==="..eventObject.type)
    if eventObject.type == "start" then

    elseif eventObject.type == "loopComplete" then

    elseif eventObject.type == "complete" then

    elseif eventObject.type == "fadeIn" then

    elseif eventObject.type == "fadeInComplete" then

    elseif eventObject.type == "fadeOut" then

    elseif eventObject.type == "fadeOutComplete" then

    elseif eventObject.type == "frameEvent" then

    elseif eventObject.type == "soundEvent" then

    end
end


--------------------------
-- 触控
-------------------------- 

function M:onTouchBegan(x, y, touches)
    --  触控有效
    return SIGN_TOUCH_BEGAN_SWALLOWS
end

function M:onTouchMoved(x, y, touches)
    --  
end

function M:onTouchEnded(x, y, touches)
    --  
end



--------------------------
-- 功能函数
--------------------------




--------------------------
-- 属性
--------------------------




--------------------------
-- 父类重写
--------------------------




----------------------------------------------------
-- 析构
--------------------------

function M:onDestructor()
    -- [超类调用]
M.super.onDestructor(self)
    -- [本类调用]

end








return M






